package june15_13;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class DigitPossibs 
{
//	1 - 2,4,5
//	2 - 1,3,4,5,6
//	3 - 2,5,6
//	4 - 1,2,5,7,8
//	5 - 1,2,3,4,6,7,8,9
//	6 - 2,3,5,8,9
//	7 - 4,5,8,0
//	8 - 4,5,6,7,9,0
//	9 - 5,6,8,0
//	0 - 7,8,9

	static int expectedLength = 9;
	static int count = 0;
	static HashMap<Integer, List<Integer>> theMap = new HashMap<Integer, List<Integer>>();
	
	public static void main(String[] args) 
	{
		
		
		theMap.put(0, Arrays.asList(new Integer[]{7,8,9}));
		theMap.put(1, Arrays.asList(new Integer[]{2,4,5}));
		theMap.put(2, Arrays.asList(new Integer[]{1,3,4,5,6}));
		theMap.put(3, Arrays.asList(new Integer[]{2,5,6}));
		theMap.put(4, Arrays.asList(new Integer[]{1,2,5,7,8}));
		theMap.put(5, Arrays.asList(new Integer[]{1,2,3,4,6,7,8,9}));
		theMap.put(6, Arrays.asList(new Integer[]{2,3,5,8,9}));
		theMap.put(7, Arrays.asList(new Integer[]{4,5,8,0}));
		theMap.put(8, Arrays.asList(new Integer[]{4,5,6,7,9,0}));
		theMap.put(9, Arrays.asList(new Integer[]{5,6,8,0}));
		
		
		for (int index = 0; index <= 9; index++)
		{
			String digitStr = index + "";
			printUptoNDigit (index, digitStr, expectedLength);
		}
	}

	static void printUptoNDigit(int index, String digitStr, int length)
	{
		if (digitStr.length() == length)
		{
			count++;
			System.out.println (count + ": " + digitStr);
			return;
		}

		List<Integer> thePossibilityList = theMap.get(index);

		for (Integer theNewIndex : thePossibilityList)
		{	
			if(digitStr.indexOf(theNewIndex+"") == -1)
				printUptoNDigit(theNewIndex, "" + digitStr + theNewIndex, length);
		}
	}
 	
}
